﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Business;

namespace QuanLyNhaSach
{
    public partial class NhapSachMoi : Form
    {
        public NhapSachMoi()
        {
            InitializeComponent();
        }
        private DataGridViewComboBoxColumn comboboxColumnTheLoai;
        private void SetUpDataGridViewSachMoi()
        {
            dgvSachMoi.BackgroundColor = Color.White;
            dgvSachMoi.ColumnCount =4;
            dgvSachMoi.Columns[0].Name = "Mã Sách";
            dgvSachMoi.Columns[1].Name = "Tên Sách";
            dgvSachMoi.Columns[2].Name = "Số Lượng Tồn";
            dgvSachMoi.Columns[3].Name = "Đơn Giá";
                       
            dgvSachMoi.Columns[0].ReadOnly = true;


            dgvSachMoi.Columns[0].Width = 80;
            dgvSachMoi.Columns[1].Width = 240;

            dgvSachMoi.AllowUserToAddRows = true;
            dgvSachMoi.AllowUserToDeleteRows = false;
            dgvSachMoi.MultiSelect = false;
            dgvSachMoi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvSachMoi.DefaultCellStyle.SelectionBackColor = Color.Ivory; //Color.Silver;
            dgvSachMoi.DefaultCellStyle.SelectionForeColor = Color.Black;

           // DataGridViewComboBoxColumn 
                comboboxColumnTheLoai = new DataGridViewComboBoxColumn();
            comboboxColumnTheLoai.HeaderText = "Thể Loại";
            comboboxColumnTheLoai.Name = "Thể Loại";
          
            
            comboboxColumnTheLoai.DataSource = TheLoaiService.GetAllTheLoai();
            comboboxColumnTheLoai.DisplayMember = TheLoaiColumns.TenTheLoai;
          //  comboboxColumnTheLoai.ValueMember = TheLoaiColumns.MaTheLoai;
            comboboxColumnTheLoai.ValueMember = TheLoaiColumns.TenTheLoai;
            MessageBox.Show(comboboxColumnTheLoai.State.ToString());
            


            DataGridViewComboBoxColumn comboboxColumnTacGia = new DataGridViewComboBoxColumn();
            comboboxColumnTacGia.HeaderText = "Tác Giả";
            comboboxColumnTacGia.Name = "Tác Giả";
            comboboxColumnTacGia.DataSource = TacGiaService.GetAllTacGia();
            comboboxColumnTacGia.DisplayMember = TacGiaColumns.TenTacGia;
           // comboboxColumnTacGia.ValueMember = TacGiaColumns.MaTacGia;
            comboboxColumnTacGia.ValueMember = TacGiaColumns.TenTacGia;

           

            DataGridViewComboBoxColumn comboboxColumnNhaXuatBan = new DataGridViewComboBoxColumn();
            comboboxColumnNhaXuatBan.HeaderText = "Nhà Xuất Bản";
            comboboxColumnNhaXuatBan.Name = "Nhà Xuất Bản";
            comboboxColumnNhaXuatBan.DataSource = NhaXuatBanService.GetAllNhaXuatBan();
            comboboxColumnNhaXuatBan.DisplayMember = NhaXuatBanColumns.TenNhaXuatBan;
         //   comboboxColumnNhaXuatBan.ValueMember = NhaXuatBanColumns.MaNhaXuatBan;
            comboboxColumnNhaXuatBan.ValueMember = NhaXuatBanColumns.TenNhaXuatBan ;

           dgvSachMoi.Columns.Insert(2, comboboxColumnTheLoai);
           dgvSachMoi.Columns.Insert(3,comboboxColumnTacGia);
            dgvSachMoi.Columns.Insert(4, comboboxColumnNhaXuatBan);
        
            
        
         /*   MessageBox.Show(dgvSachMoi.Columns[0].Name.ToString()); 
            MessageBox.Show(dgvSachMoi.Columns[1].Name.ToString()); 
            MessageBox.Show(dgvSachMoi.Columns[2].Name.ToString());
            MessageBox.Show(dgvSachMoi.Columns[3].Name.ToString());
            MessageBox.Show(dgvSachMoi.Columns[4].Name.ToString());
            MessageBox.Show(dgvSachMoi.Columns[5].Name.ToString());
            MessageBox.Show(dgvSachMoi.Columns[6].Name.ToString()); */
            
          
          
        }
        public void ReLoadTheLoai(object sender,EventArgs e)
        {
            comboboxColumnTheLoai.DataSource = TheLoaiService.GetAllTheLoai();
            comboboxColumnTheLoai.DisplayMember = TheLoaiColumns.TenTheLoai;
            //  comboboxColumnTheLoai.ValueMember = TheLoaiColumns.MaTheLoai;
            comboboxColumnTheLoai.ValueMember = TheLoaiColumns.TenTheLoai;
        }
        private void NhapSachMoi_Load(object sender, EventArgs e)
        {
            SetUpDataGridViewSachMoi();
           


        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (dgvSachMoi.Rows.Count - 1 != 0)
            {
                for (int i = 0; i < dgvSachMoi.Rows.GetRowCount(DataGridViewElementStates.Visible)-1; i++)
                {
                    SachDTO sachDTO = new SachDTO();
                  //  sachDTO.MaSach = NumberIdService.NewNumber("SACH");
                  //  NumberIdService.Update("SACH");
                   // sachDTO.TenSach = dgvSachMoi.Rows[i].Cells[1].Value.ToString();
                  //  sachDTO.MaTacGia=

                    MessageBox.Show(i.ToString());
                  //  MessageBox.Show(dgvSachMoi.Rows[i].Cells[0].Value.ToString());
                    try
                    {
                        MessageBox.Show(dgvSachMoi.Rows[i].Cells["Tên Sách"].Value.ToString());
                        MessageBox.Show(dgvSachMoi.Rows[i].Cells["Thể Loại"].Value.ToString());
                        MessageBox.Show(dgvSachMoi.Rows[i].Cells["Tác Giả"].Value.ToString());
                        MessageBox.Show(dgvSachMoi.Rows[i].Cells["Nhà Xuất Bản"].Value.ToString());
                        MessageBox.Show(dgvSachMoi.Rows[i].Cells["Số Lượng Tồn"].Value.ToString());
                        MessageBox.Show(dgvSachMoi.Rows[i].Cells["Đơn Giá"].Value.ToString());
                    }
                    catch
                    {
                        MessageBox.Show("Dữ liệu không được lưu");
                        dgvSachMoi.Rows[i].Selected = true;
                        break;
                    }
                   


                }
            }
        }

        private void dgvSachMoi_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            //MessageBox.Show("rowsadded");
        }

        private void dgvSachMoi_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            //MessageBox.Show("useraddedrow");
        }

        private void dgvSachMoi_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvSachMoi.SelectedRows[0].IsNewRow == false)
            {
                MessageBox.Show("rowleave");
            }
        }

        
        private void NhapSachMoi_AutoValidateChanged(object sender, EventArgs e)
        {
            MessageBox.Show("autovalidatechanged");
        }

       

        
    }
}
